package com.lsege.controller.backstage;

import com.github.pagehelper.PageHelper;
import com.lsege.entity.JsonResult;
import com.lsege.entity.Page;
import com.lsege.entity.backstage.MutualAid;
import com.lsege.entity.backstage.MutualAidApply;
import com.lsege.service.backstage.MutualAidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Administrator on 2017/7/29 0029.
 */
@RestController
@RequestMapping(value = "/mutualAid")
public class MutualAidController {
    @Autowired
    MutualAidService mutualAidService;

    /**
     * 获取互助单
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("getMutualAidList")
    public JsonResult getMutualAidList(Integer pageNum,Integer pageSize){
        JsonResult json=new JsonResult();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<MutualAid> list=mutualAidService.getMutualAidList();
            Long total=mutualAidService.getMutuaiAidCount();
            json.setData(new Page(list,total,pageSize,pageNum));
            json.setSuccess(true);
            json.setMessage("获取成功");
        }catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("获取失败");
        }
        return json;
    }

    /**
     * 查看互助申请列表
     * @param pageNum
     * @param pageSize
     * @param applyState
     * @return
     */
    @GetMapping("getMutualAidApply")
    public JsonResult getMutualAidApply(Integer pageNum,Integer pageSize,Integer applyState){
        JsonResult json=new JsonResult();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<MutualAidApply> list=mutualAidService.getMutualAidApply(applyState);
            Long total=mutualAidService.getMutualAidApplyCount(applyState);
            json.setData(new Page(list,total,pageSize,pageNum));
            json.setSuccess(true);
            json.setMessage("获取成功");
        }catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("获取失败");
        }
        return json;
    }

    /**
     * 获取往期互助申请列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("getOverMutualAidApply")
    public JsonResult getOverMutualAidApply(Integer pageNum,Integer pageSize){
        JsonResult json=new JsonResult();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<MutualAidApply> list=mutualAidService.getOverMutualAidApply();
            Long total=mutualAidService.getOverMutualAidApplyCount();
            json.setData(new Page(list,total,pageSize,pageNum));
            json.setSuccess(true);
            json.setMessage("获取成功");
        }catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("获取失败");
        }

        return json;
    }

    /**
     * 驳回提交的申请
     * @param mutualAidApply
     * @return
     */
    @PostMapping("rejectApply")
    public JsonResult rejectApply(MutualAidApply mutualAidApply){
        JsonResult json=new JsonResult();
        try {
            mutualAidService.rejectApply(mutualAidApply);
            mutualAidService.rejectApplyReason(mutualAidApply);
            json.setSuccess(true);
            json.setMessage("该申请已驳回");
        }catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("驳回失败");
        }
        return json;
    }

    /**
     * 通过大病互助申请
     * @param mutualAidApply
     * @return
     */
    @PostMapping("allowApply")
    public JsonResult allowApply(MutualAidApply mutualAidApply){
        JsonResult json=new JsonResult();
        try {
            Long countMoney=mutualAidService.getApplyMoney(mutualAidApply);
            Long num=mutualAidService.getMutualAidCount();
            Long avgMoney= Long.valueOf(countMoney/num);
            Float avg=Float.valueOf(String.valueOf(avgMoney))/100;
            mutualAidService.judgeCost(avgMoney);
            mutualAidService.changeApplyMoney(avgMoney);
            mutualAidService.changeMutualAidState();
            mutualAidService.changeApplyState(mutualAidApply.getMutualCode());
            mutualAidService.allowApply(mutualAidApply);
            mutualAidService.changeCannotToCan();
            MutualAidApply mutualAidApply1=new MutualAidApply();
            mutualAidApply1.setCountMoney(countMoney/100);
            mutualAidApply1.setCountPeople(num);
            mutualAidApply1.setAvgMoney(avg);
            json.setData(mutualAidApply1);
            json.setSuccess(true);
            json.setMessage("该申请已通过");
        }catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("申请通过失败");
        }
        return json;
    }
}
